package com.dtm.client;

import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;

import com.dtm.adapter.ServerAdapter;
import com.dtm.client.thread.UpdateStatusListener;
import com.dtm.server.GameStartListener;
import com.dtm.server.ServerThread;
import com.dtm.server.SomethingHappenedInterface;
import com.dtm.ui.MainActivity;

public class SensorThread extends Thread implements SensorEventListener {
	
	public static final int FACE_UP = 1;
	public static final int FACE_DOWN = 2;
	
	private Vibrator vibrator;
	
	private Context context;

	private SensorManager sensorManager;
	
	private boolean accelerometerPresent;
	private Sensor accelerometerSensor;
	private int status;
	
	private UpdateStatusListener updateStatus;
	private GameStartListener gameStartListener;
	private SomethingHappenedInterface smtHpnedI;
	
	private Vector<Client> clientVector = null;
	
	public void setClientVector(Vector<Client> clientVector) {
		this.clientVector = clientVector;
	}
	
	public void setUpdateStatus(UpdateStatusListener updateStatus) {
		this.updateStatus = updateStatus;
	}
	
	public void setGameStartListener(GameStartListener gameStartListener) {
		this.gameStartListener = gameStartListener;
	}
	
	public void setSomethingHappennedListener(SomethingHappenedInterface smtHpnedI) {
		this.smtHpnedI = smtHpnedI;
	}
	
	public SensorThread(Context context) {
		this.context = context;
	}
	
	@Override
	public void run() {
		
		vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
		sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
		status = FACE_UP;
		
		List<Sensor> sensorList = sensorManager
				.getSensorList(Sensor.TYPE_ACCELEROMETER);
		
		if (sensorList.size() > 0) {
			accelerometerPresent = true;
			accelerometerSensor = sensorList.get(0);
		} else {
			accelerometerPresent = false;
		}
		
		if (accelerometerPresent) {
			sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}
	
	private boolean isGameStart() {
		for (int i = 0; i < clientVector.size(); i++) {
			if (clientVector.get(i).getStatus() == ClientConnectedThread.CONNECTED) {
				return false;
			}
		}
		return true;
	}

	@Override
	public void onSensorChanged(SensorEvent arg0) {
		float z_value = arg0.values[2];
		if (z_value >= -6f && z_value <= 14.5 && status == FACE_DOWN) {
			status = FACE_UP;
			if (clientVector == null) {
				updateStatus.onUpdateStatus(FACE_UP);	
			} else {
				if (ServerThread.gameStatus == ClientConnectedThread.GAME_IN_PROGRESS) {
					smtHpnedI.onEndGame(MainActivity.userName + " losed");
				}
			}
			
		} else if (z_value >= -10.2f && z_value <= -9.8f && status == FACE_UP) {
			status = FACE_DOWN;
			if (clientVector != null) {
				if (isGameStart()) {
					vibrateForDown();
					gameStartListener.onStart();
				}
			} else {
				vibrateForDown();
				updateStatus.onUpdateStatus(FACE_DOWN);
			}
		}
	}
	
	private void vibrateForDown() {
		long[] pattern = new long[]{0,150};
        vibrator.vibrate(pattern,-1);
		
	}
	
	public void vibrateForEndGame() {
		long[] pattern = new long[]{0, 150, 200, 150, 200, 150};
		vibrator.vibrate(pattern, -1);
	}
	
}
